package com.zy.controller;
import com.zy.entity.ParkingLot;
import com.zy.entity.PricingStandard;
import com.zy.service.ParkingLotService;
import org.springframework.ui.Model;
import com.zy.service.PricingStandardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/parking")
public class ParkingLotController {
    ParkingLotService service;
    PricingStandardService pricingStandardService;
    @Autowired
    public ParkingLotController(ParkingLotService service
            , PricingStandardService pricingStandardService){
        this.service=service;
        this.pricingStandardService=pricingStandardService;
    }
    @RequestMapping("/list")
    public  String list(Model model){
        List<ParkingLot> entities=service.getAll();
        model.addAttribute("list",entities);//model打包成list属性完成list对象的值的传递
        return "/parking/list";
    }
    @RequestMapping(value="/insert")
    public String insert(Model model){
        model.addAttribute("parking",new ParkingLot());
        List<PricingStandard> pricingList=pricingStandardService.getAll();
        model.addAttribute("pricingList",pricingList);
        return "/parking/edit";
    }

    @RequestMapping(value="/edit/{id}")
    public String edit(@PathVariable("id") Long id, Model model){
        ParkingLot entity=service.getById(id);
        model.addAttribute("parking",entity);
        List<PricingStandard> pricingList=pricingStandardService.getAll();
        model.addAttribute("pricingList",pricingList);
        return "/parking/edit";
    }
    @RequestMapping(value = "/detail/{id}")
    public String detail(@PathVariable("id") Long id, Model model){
        ParkingLot entity=service.getById(id);
        model.addAttribute("parking",entity);
        return "/parking/detail";
    }
    @RequestMapping(value = "/delete/{id}")
    public String delete(@PathVariable("id") Long id){
        boolean  result=service.delete(id);
        return "redirect:/parking/list";
    }
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public String save(@ModelAttribute("pricing") ParkingLot entity, @RequestParam("pricingId") Long pricingId){
        PricingStandard pricing=pricingStandardService.getById(pricingId);
        entity.setPricingStandard(pricing);
        boolean result=entity.getId()!=null?service.update(entity):service.insert(entity);
        return "redirect:/parking/list";
}
}

